using System;
using System.Configuration;
using System.Data;
using digit.Core;

namespace SMS.MediaManager
{
	public class VoiceManager
	{
		#region CONSTRUCTORS
		static VoiceManager()
		{
			_data = new VoiceData(ConfigurationManager.ConnectionStrings["SMS"].ConnectionString);
			_error = "";
		}
		#endregion
		
		#region ATTRIBUTES
		private static VoiceData _data;
		private static string _error;
		#endregion
		
		#region PROPERTIES
		public static string ErrorMessage
		{
			get
			{
				return _error;
			}
		}
		
		#endregion
		
		#region METHODS
		public static DataTable Insert( string voiceName1,  string voiceName2,  string voiceName3,  string voiceName4,  int categoryID,  string uRL1,  string uRL2,  string uRL3,  string uRL4,  string uRL5,  string uRL6,  string author,  string singer,  string voiceDemoURL,  string location,  long sMSCount,  string createdBy,  DateTime createdDate,  byte status)
		{
			_error = "";
			try
			{
				object[] values = new object[19];
				values[0] = Util.ReplaceNullTo(voiceName1, DBNull.Value);
				values[1] = Util.ReplaceNullTo(voiceName2, DBNull.Value);
				values[2] = Util.ReplaceNullTo(voiceName3, DBNull.Value);
				values[3] = Util.ReplaceNullTo(voiceName4, DBNull.Value);
				values[4] = categoryID;
				values[5] = Util.ReplaceNullTo(uRL1, DBNull.Value);
				values[6] = Util.ReplaceNullTo(uRL2, DBNull.Value);
				values[7] = Util.ReplaceNullTo(uRL3, DBNull.Value);
				values[8] = Util.ReplaceNullTo(uRL4, DBNull.Value);
				values[9] = Util.ReplaceNullTo(uRL5, DBNull.Value);
				values[10] = Util.ReplaceNullTo(uRL6, DBNull.Value);
				values[11] = Util.ReplaceNullTo(author, DBNull.Value);
				values[12] = Util.ReplaceNullTo(singer, DBNull.Value);
				values[13] = Util.ReplaceNullTo(voiceDemoURL, DBNull.Value);
				values[14] = location;
				values[15] = Util.ReplaceNullTo(sMSCount, DBNull.Value);
				values[16] = Util.ReplaceNullTo(createdBy, DBNull.Value);
				values[17] = Util.ReplaceNullTo(createdDate, DBNull.Value);
				values[18] = Util.ReplaceNullTo(status, DBNull.Value);
		
				return _data.Insert(values);
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return null;
			}
		}
		
		public static DataTable Insert( VoiceInfo info)
		{
			_error = "";
			try
			{
				object[] values = new object[19];
				values[0] = Util.ReplaceNullTo(info.VoiceName1, DBNull.Value);
				values[1] = Util.ReplaceNullTo(info.VoiceName2, DBNull.Value);
				values[2] = Util.ReplaceNullTo(info.VoiceName3, DBNull.Value);
				values[3] = Util.ReplaceNullTo(info.VoiceName4, DBNull.Value);
				values[4] = info.CategoryID;
				values[5] = Util.ReplaceNullTo(info.URL1, DBNull.Value);
				values[6] = Util.ReplaceNullTo(info.URL2, DBNull.Value);
				values[7] = Util.ReplaceNullTo(info.URL3, DBNull.Value);
				values[8] = Util.ReplaceNullTo(info.URL4, DBNull.Value);
				values[9] = Util.ReplaceNullTo(info.URL5, DBNull.Value);
				values[10] = Util.ReplaceNullTo(info.URL6, DBNull.Value);
				values[11] = Util.ReplaceNullTo(info.Author, DBNull.Value);
				values[12] = Util.ReplaceNullTo(info.Singer, DBNull.Value);
				values[13] = Util.ReplaceNullTo(info.VoiceDemoURL, DBNull.Value);
				values[14] = info.Location;
				values[15] = Util.ReplaceNullTo(info.SMSCount, DBNull.Value);
				values[16] = Util.ReplaceNullTo(info.CreatedBy, DBNull.Value);
				values[17] = Util.ReplaceNullTo(info.CreatedDate, DBNull.Value);
				values[18] = Util.ReplaceNullTo(info.Status, DBNull.Value);
		
				return _data.Insert(values);
			}
			catch(Exception ex)
			{
				_error = ex.Message;
                return null;
			}
		}
		
		public static bool Update( long voiceID,  string voiceName1,  string voiceName2,  string voiceName3,  string voiceName4,  int categoryID,  string uRL1,  string uRL2,  string uRL3,  string uRL4,  string uRL5,  string uRL6,  string author,  string singer,  string voiceDemoURL,  string location, byte status)
		{
			_error = "";
			try
			{
				object[] values = new object[17];
				values[0] = voiceID;
				values[1] = Util.ReplaceNullTo(voiceName1, DBNull.Value);
				values[2] = Util.ReplaceNullTo(voiceName2, DBNull.Value);
				values[3] = Util.ReplaceNullTo(voiceName3, DBNull.Value);
				values[4] = Util.ReplaceNullTo(voiceName4, DBNull.Value);
				values[5] = categoryID;
				values[6] = Util.ReplaceNullTo(uRL1, DBNull.Value);
				values[7] = Util.ReplaceNullTo(uRL2, DBNull.Value);
				values[8] = Util.ReplaceNullTo(uRL3, DBNull.Value);
				values[9] = Util.ReplaceNullTo(uRL4, DBNull.Value);
				values[10] = Util.ReplaceNullTo(uRL5, DBNull.Value);
				values[11] = Util.ReplaceNullTo(uRL6, DBNull.Value);
				values[12] = Util.ReplaceNullTo(author, DBNull.Value);
				values[13] = Util.ReplaceNullTo(singer, DBNull.Value);
				values[14] = Util.ReplaceNullTo(voiceDemoURL, DBNull.Value);
				values[15] = location;
				values[16] = Util.ReplaceNullTo(status, DBNull.Value);
		
				return _data.Update(values);
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return false;
			}
		}
		
		public static bool Update( VoiceInfo info)
		{
			_error = "";
			try
			{
				object[] values = new object[20];
				values[0] = info.VoiceID;
				values[1] = Util.ReplaceNullTo(info.VoiceName1, DBNull.Value);
				values[2] = Util.ReplaceNullTo(info.VoiceName2, DBNull.Value);
				values[3] = Util.ReplaceNullTo(info.VoiceName3, DBNull.Value);
				values[4] = Util.ReplaceNullTo(info.VoiceName4, DBNull.Value);
				values[5] = info.CategoryID;
				values[6] = Util.ReplaceNullTo(info.URL1, DBNull.Value);
				values[7] = Util.ReplaceNullTo(info.URL2, DBNull.Value);
				values[8] = Util.ReplaceNullTo(info.URL3, DBNull.Value);
				values[9] = Util.ReplaceNullTo(info.URL4, DBNull.Value);
				values[10] = Util.ReplaceNullTo(info.URL5, DBNull.Value);
				values[11] = Util.ReplaceNullTo(info.URL6, DBNull.Value);
				values[12] = Util.ReplaceNullTo(info.Author, DBNull.Value);
				values[13] = Util.ReplaceNullTo(info.Singer, DBNull.Value);
				values[14] = Util.ReplaceNullTo(info.VoiceDemoURL, DBNull.Value);
				values[15] = info.Location;
				values[16] = Util.ReplaceNullTo(info.SMSCount, DBNull.Value);
				values[17] = Util.ReplaceNullTo(info.CreatedBy, DBNull.Value);
				values[18] = Util.ReplaceNullTo(info.CreatedDate, DBNull.Value);
				values[19] = Util.ReplaceNullTo(info.Status, DBNull.Value);
		
				return _data.Update(values);
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return false;
			}
		}
		
		public static bool Delete( long voiceID)
		{
			_error = "";
			try
			{
				object[] values = { voiceID };
				return _data.Delete(values);
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return false;
			}
		}
		
		public static bool Delete( VoiceInfo info)
		{
			_error = "";
			try
			{
				object[] values = { info.VoiceID };
				return _data.Delete(values);
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return false;
			}
		}

        public static DataTable SelectInfo(long mp3ID)
        {
            _error = "";
            try
            {
                object[] values = { mp3ID };
                return _data.Select(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return null;
            }
        }
		
		public static VoiceInfo Select( long voiceID)
		{
			_error = "";
			try
			{
				object[] values = { voiceID };
				DataTable dTable = _data.Select(values);
				if (dTable.Rows.Count < 1) return null;
		
				
				VoiceInfo info = new VoiceInfo(dTable.Rows[0]);
				return info;
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return null;
			}
		}
		
		public static DataTable SelectAll()
		{
			_error = "";
			try
			{
				return _data.SelectAll();
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return null;
			}
		}
		
		public static DataTable Search( string whereClause,  string sortOrder,  int pageIndex,  int pageSize)
		{
			_error = "";
			try
			{
				object[] values = { whereClause, sortOrder };
				int index = (pageIndex * pageSize) - 1;
				if (index < 0) index = 0;
				return _data.Search(values, index, pageSize);
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return null;
			}
		}
		
		public static DataTable Search( string whereClause,  string sortOrder)
		{
			return VoiceManager.Search(whereClause, sortOrder, 0, 0);
		}
		
		public static DataTable Search( int pageIndex,  int pageSize)
		{
			return VoiceManager.Search( "", "", pageIndex, pageSize);
		}

        public static DataTable SearchBy(int intCateId, string strName, DateTime fromDate, DateTime toDate)
        {
            _error = "";
            try
            {
                object[] values = new object[4];
                values[0] = intCateId;
                values[1] = strName;
                values[2] = fromDate;
                values[3] = toDate;

                return _data.SearchBy(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return null;
            }
        }

        public static bool Approve(int mp3Id, byte approveValue)
        {
            _error = "";
            try
            {
                object[] values = new object[2];
                values[0] = mp3Id;
                values[1] = approveValue;
                return _data.Approve(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }
		
		#endregion
		
	}
}